package com.cmcc.passenger.ui.profile

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.cmcc.passenger.R
import com.cmcc.passenger.config.PassPath
import com.cmcc.passenger.data.entity.FavoriteAddress
import com.cmcc.passenger.databinding.ActivityFavoriteBinding
import com.cmcc.passenger.ui.base.BasicActivity
import com.cmcc.passenger.ui.fragment.AppProgressDialog
import com.cmcc.passenger.ui.profile.viewmodel.AddressBookViewModel
import com.cmcc.shared.utils.DisplayUtil
import com.cmcc.shared.utils.L

/**
 * 常用地址管理
 */
@Route(path = PassPath.FAVORITE_ADDRESS)
class AddressBookActivity : BasicActivity() {
    private lateinit var viewModel: AddressBookViewModel
    private var adapter: AddressAdapter? = null
    private var progressDialog: AppProgressDialog? = null

    override fun initialize(savedInstanceState: Bundle?) {
        val binding = DataBindingUtil.setContentView<ActivityFavoriteBinding>(
            this,
            R.layout.activity_favorite
        )
        viewModel = ViewModelProviders.of(this).get(AddressBookViewModel::class.java)
        progressDialog = AppProgressDialog(this)
        binding.recyclerView.layoutManager = LinearLayoutManager(this)
        binding.toolbar.title.text = getString(R.string.address_book_title)
        binding.toolbar.toolbar.setNavigationOnClickListener { finish() }


        binding.recyclerView.addItemDecoration(
            SpaceItemDecoration(
                DisplayUtil.dp2px(
                    this,
                    8f
                ).toInt()
            )
        )
        adapter = AddressAdapter()
        binding.recyclerView.adapter = adapter

        val addressBook = mutableListOf<FavoriteAddress>()
//        addressBook.add(
//            FavoriteAddress(
//                null,
//                null,
//                null,
//                null,
//                null,
//                getString(R.string.home_address_title)
//            )
//        )
//        addressBook.add(
//            FavoriteAddress(
//                null,
//                null,
//                null,
//                null,
//                null,
//                getString(R.string.company_address_title)
//            )
//        )
//        addressBook.add(
//            FavoriteAddress(
//                null,
//                null,
//                null,
//                null,
//                null,
//                getString(R.string.favorite_address_title)
//            )
//        )
        //adapter?.submitList(addressBook)
        adapter?.setOnDeleteClickListener(object : AddressAdapter.OnDeleteClickListener {
            override fun onClick(address: FavoriteAddress) {
                progressDialog?.show()
                viewModel.deleteAddress(address.id!!)
            }

        })
        viewModel.getAddress()

        viewModel.address.observe(this, Observer { address ->
            L.d(address.toString())
            adapter = AddressAdapter()
            adapter?.setOnDeleteClickListener(object : AddressAdapter.OnDeleteClickListener {
                override fun onClick(address: FavoriteAddress) {
                    progressDialog?.show()
                    viewModel.deleteAddress(address.id!!)
                }

            })
            binding.recyclerView.adapter = adapter

            val addressBook = mutableListOf<FavoriteAddress>()

            addressBook.add(
                FavoriteAddress(
                    null,
                    null,
                    null,
                    null,
                    null,
                    getString(R.string.home_address_title)
                )
            )
            if (null != address.homes) {
                addressBook.addAll(address.homes)
            }
            addressBook.add(
                FavoriteAddress(
                    null,
                    null,
                    null,
                    null,
                    null,
                    getString(R.string.company_address_title)
                )
            )
            if(null != address.companys){
                addressBook.addAll(address.companys)
            }
            addressBook.add(
                FavoriteAddress(
                    null,
                    null,
                    null,
                    null,
                    null,
                    getString(R.string.favorite_address_title)
                )
            )
            if(null != address.commons){
                addressBook.addAll(address.commons)
            }

            adapter?.submitList(addressBook)

        })

        viewModel.deleteAddress.observe(this, Observer {
            progressDialog?.dismiss()
            viewModel.getAddress()
        })
    }

    override fun initObserver(savedInstanceState: Bundle?) {

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        viewModel.getAddress()
    }

    companion object {
        const val REQUEST_ADD_HOME_CODE = 0x001
        const val REQUEST_ADD_COMPANY_CODE = 0x002
        const val REQUEST_ADD_FAVORITY_CODE = 0x003
    }
}